#ifndef DISPLAYUSER_C
#define DISPLAYUSER_C


#include "displayuser.h"
#include "displaydriver.h"
#include "Imagemap.c"
#include "Image.h"
#include "machine_stat.h"

/*-------------------------------------------------------------------*
 | Set each entry in the table with the segment patterns for the 
 | corresponding character.
 *-------------------------------------------------------------------*/

uint8_t const IMAGE_CHARACTER_SET_0[] = 
{
    {BYTE0(PATTERN_0)},
    {BYTE0(PATTERN_1)},
    {BYTE0(PATTERN_2)},
    {BYTE0(PATTERN_3)},
    {BYTE0(PATTERN_4)},
    {BYTE0(PATTERN_5)},
    {BYTE0(PATTERN_6)},
    {BYTE0(PATTERN_7)},
    {BYTE0(PATTERN_8)},
    {BYTE0(PATTERN_9)},
    {BYTE0(PATTERN_MASK)},		/* 10 */
    {BYTE0(PATTERN_BLANK)},		/* 11 */
    {BYTE0(PATTERN_DASH)},		/* 12 */
    {BYTE0(PATTERN_A)},			/* 13 */
    {BYTE0(PATTERN_b)},			/* 14 */
    {BYTE0(PATTERN_C)},			/* 15 */
    {BYTE0(PATTERN_d)},			/* 16 */
    {BYTE0(PATTERN_E)},			/* 17 */
	{BYTE0(PATTERN_F)},			/* 18 */
	{BYTE0(PATTERN_G)},			/* 19 */
	{BYTE0(PATTERN_H)},			/* 20 */
	{BYTE0(PATTERN_i)},			/* 21 */
	{BYTE0(PATTERN_J)},			/* 22 */
	{BYTE0(PATTERN_K)},			/* 23 */
    {BYTE0(PATTERN_L)},			/* 24 */
    {BYTE0(PATTERN_M)},			/* 25 */
    {BYTE0(PATTERN_n)},			/* 26 */
	{BYTE0(PATTERN_o)},			/* 27 */
    {BYTE0(PATTERN_P)},			/* 28 */
	{BYTE0(PATTERN_Q)},			/* 29 */
    {BYTE0(PATTERN_r)},			/* 30 */
    {BYTE0(PATTERN_S)},			/* 31 */
    {BYTE0(PATTERN_t)},			/* 32 */
    {BYTE0(PATTERN_U)},			/* 33 */
    {BYTE0(PATTERN_v)},			/* 34 */
	{BYTE0(PATTERN_W)},			/* 35 */
	{BYTE0(PATTERN_X)},			/* 36 */
	{BYTE0(PATTERN_Y)},			/* 37 */
    {BYTE0(PATTERN_Z)},         /* 38 */
	
    {BYTE0(PATTERN__1)}, 
    {BYTE0(PATTERN__2)}, 
    {BYTE0(PATTERN__3)}, 
	
	{BYTE0(PATTERN_I)},
	{BYTE0(PATTERN_u)},
	{BYTE0(PATTERN_h)},
	{BYTE0(PATTERN_N)},
	
	{BYTE0(PATTERN_DEGREE)},
};


uint8_t displayUserBuffer[2];
uint8_t displayBlinkBit;

uint8_t displayUserCount;
uint8_t displayUserPromptNum;

uint8_t displayAlternateState;
uint8_t displayAlternateCount;


void UserConsumerDisplay(void);

void displayUserBurnStatus(void);

void displayUserEcoStatus(void);

void displayUserAlternate(void);

void displayUserInitialize(void)
{

}
//------------------------------------------------------------------------------------
void displayUserDigitalField(void)
{
	uint8_t buffer[2];
	uint8_t i;
	
	for (i = 0; i < 2; i++)
	    buffer[i] = 0;
	 
	buffer[0] = IMAGE_CHARACTER_SET_0[displayUserBuffer[0]];
	buffer[1] = IMAGE_CHARACTER_SET_0[displayUserBuffer[1]];
	
	if (buffer[0] & 0x01)
	{
		if ( displayBlinkBit & 0x01 )
		    ImagePromptSet(DIGITAL1_A, IMAGE_REQUEST_BLINK); //IMAGE_REQUEST_BLINK 0x10
		else
	        ImagePromptSet(DIGITAL1_A, ON);
	}
	if (buffer[0] & 0x02)
	{
		if ( displayBlinkBit & 0x01 )
		    ImagePromptSet(DIGITAL1_B, IMAGE_REQUEST_BLINK);
		else
	        ImagePromptSet(DIGITAL1_B, ON);
	}
	if (buffer[0] & 0x04)
	{
		if ( displayBlinkBit & 0x01 )
		    ImagePromptSet(DIGITAL1_C, IMAGE_REQUEST_BLINK);
		else
	        ImagePromptSet(DIGITAL1_C, ON);
	}
	if (buffer[0] & 0x08)
	{
		if ( displayBlinkBit & 0x01 )
		    ImagePromptSet(DIGITAL1_D, IMAGE_REQUEST_BLINK);
		else
	        ImagePromptSet(DIGITAL1_D, ON);
	}
	if (buffer[0] & 0x10)
	{
		if ( displayBlinkBit & 0x01 )
		    ImagePromptSet(DIGITAL1_E, IMAGE_REQUEST_BLINK);
		else
	        ImagePromptSet(DIGITAL1_E, ON);
	}
	if (buffer[0] & 0x20)
	{
		if ( displayBlinkBit & 0x01 )
		    ImagePromptSet(DIGITAL1_F, IMAGE_REQUEST_BLINK);
		else
	        ImagePromptSet(DIGITAL1_F, ON);
	}
	if (buffer[0] & 0x40)
	{
		if ( displayBlinkBit & 0x01 )
		    ImagePromptSet(DIGITAL1_G, IMAGE_REQUEST_BLINK);
		else
	        ImagePromptSet(DIGITAL1_G, ON);
	}
	
	if (buffer[1] & 0x01)
	{
		if ( displayBlinkBit & 0x02 )
		    ImagePromptSet(DIGITAL2_A, IMAGE_REQUEST_BLINK);
		else
	        ImagePromptSet(DIGITAL2_A, ON);
	}
	if (buffer[1] & 0x02)
	{
		if ( displayBlinkBit & 0x02 )
		    ImagePromptSet(DIGITAL2_B, IMAGE_REQUEST_BLINK);
		else
	        ImagePromptSet(DIGITAL2_B, ON);
	}
	if (buffer[1] & 0x04)
	{
		if ( displayBlinkBit & 0x02 )
		    ImagePromptSet(DIGITAL2_C, IMAGE_REQUEST_BLINK);
		else
	        ImagePromptSet(DIGITAL2_C, ON);
	}
	if (buffer[1] & 0x08)
	{
		if ( displayBlinkBit & 0x02 )
		    ImagePromptSet(DIGITAL2_D, IMAGE_REQUEST_BLINK);
		else
	        ImagePromptSet(DIGITAL2_D, ON);
	}
	if (buffer[1] & 0x10)
	{
		if ( displayBlinkBit & 0x02 )
		    ImagePromptSet(DIGITAL2_E, IMAGE_REQUEST_BLINK);
		else
	        ImagePromptSet(DIGITAL2_E, ON);
	}
	if (buffer[1] & 0x20)
	{
		if ( displayBlinkBit & 0x02 )
		    ImagePromptSet(DIGITAL2_F, IMAGE_REQUEST_BLINK);
		else
	        ImagePromptSet(DIGITAL2_F, ON);
	}
	if (buffer[1] & 0x40)
	{
		if ( displayBlinkBit & 0x02 )
		    ImagePromptSet(DIGITAL2_G, IMAGE_REQUEST_BLINK);
		else
	        ImagePromptSet(DIGITAL2_G, ON);
	}
}
//--------------------------------------------------------------------------
void displayUserDigitalTemp(uint8_t temp)
{
	displayBlinkBit = 0;
	
	displayUserBuffer[0] = temp / 10;
	if (displayUserBuffer[0] == 0)
    {
        displayUserBuffer[0] = DIGITAL_BLANK; 
    }
    displayUserBuffer[1] = temp % 10;
}

void displayUserDigitalFault(void)
{
	uint8_t fault;
	
	fault = heater_info.err_type;
	displayUserBuffer[0] = (fault >> 4);
	displayUserBuffer[1] = (fault & 0x0F);
}
//--------------------------------------------------------------------------
void displayUserSegContent(void)
{
	uint8_t temp;
/*	
	if(heater_info.err_type== FAULT_ACTION) //( userGetFaultType() == FAULT_ACTION )
	{
	}
	else
	{
	    displayAlternateCount = 0;
	    displayAlternateState = 0;
	}
*/	
	switch (heater_info.err_type)//( userGetFaultType() )
	{
		case FAULT_NONE:			
	        	//temp = userGetHeatTemp();
			displayUserDigitalTemp(heater_info.SetHeatTemp);
			
			break;
		case FAULT_ACTION:
			displayUserAlternate();
			displayBlinkBit = 0;
	        break;
		case FAULT_BURNING:
		    displayUserDigitalFault();
			displayBlinkBit |= 0x01;
	        displayBlinkBit |= 0x02;
		    break;
		default:
		    break;
	}
		
	displayUserDigitalField();
}
//--------------------------------------------------------------------------
//50ms
void displayUserOperation(void)
{
	uint8_t i;
		
	for (i = 0; i < IMAGE_PROMPT_COUNT; i++)
	{
		ImagePromptSet(i, OFF);
	}
	
	
	//displayBlinkBit |= 0x01;
	//displayBlinkBit |= 0x02;
	//ImagePromptSet(IMAGE_PROMPT_1, ON);
	
	//ImagePromptSet(IMAGE_PROMPT_2, IMAGE_REQUEST_BLINK);
	
	//ImagePromptSet(IMAGE_PROMPT_8, ON);
	
	//ImagePromptSet(IMAGE_PROMPT_16, IMAGE_REQUEST_BLINK);
	
	//ImagePromptSet(displayUserPromptNum, ON);
	
	if (heater_info.power_stat==0x31)//( UserGetPowerStatus() == ON )
	{
		displayUserSegContent(); // err type and 
		
		displayUserBurnStatus(); // fire type
		
		displayUserEcoStatus();  //eco mode
	}
}
//100ms
void displayUserProcess(void)
{
	if (++displayUserCount >= 10 )
	{
		displayUserCount = 0;
		displayUserPromptNum++;
		if (displayUserPromptNum >= IMAGE_PROMPT_COUNT)
		{
			displayUserPromptNum = 0;
		}
	}
}

//------------------------------------------------------------------------------------

//------------------------------------------------------------------------------------
void UserConsumerDisplay(void)
{
}
//------------------------------------------------------------------------------------
void displayUserBurnStatus(void)
{
	if (heater_info.fire_stat)
	{
		ImagePromptSet(IMAGE_PROMPT_BURN, ON);
	}
	else
	{
		
	}
}
//------------------------------------------------------------------------------------

void displayUserEcoStatus(void)
{
	if(heater_info.eco_stat==0x31)
	{
		ImagePromptSet(IMAGE_PROMPT_ECO, ON);
	}
	else
	{
		
	}
}


//------------------------------------------------------------------------------------
void displayUserAlternate(void)
{
	
	if ( displayAlternateCount < 0xFF )
	{
		displayAlternateCount++;
	}
	
	switch (displayAlternateState)
    {
	    case ALTERNATE_STA_0: 
			displayUserDigitalTemp(heater_info.SetHeatTemp);
			if (displayAlternateCount >= TIME_DELAY_5S)
			{
				displayAlternateCount = 0;
				displayAlternateState = ALTERNATE_STA_1;
			}
		    break;
		case ALTERNATE_STA_1:
		    displayUserBuffer[0] = DIGITAL_BLANK;
			displayUserBuffer[1] = DIGITAL_BLANK;
		    if (displayAlternateCount >= TIME_DELAY_250MS)
			{
				displayAlternateCount = 0;
				displayAlternateState = ALTERNATE_STA_2;
			}
		    break;
		case ALTERNATE_STA_2:
		    displayUserDigitalFault();
			if (displayAlternateCount >= TIME_DELAY_1S)
			{
				displayAlternateCount = 0;
				displayAlternateState = ALTERNATE_STA_3;
			}
		    break;
		case ALTERNATE_STA_3:
		    displayUserBuffer[0] = DIGITAL_BLANK;
			displayUserBuffer[1] = DIGITAL_BLANK;
		    if (displayAlternateCount >= TIME_DELAY_250MS)
			{
				displayAlternateCount = 0;
				displayAlternateState = ALTERNATE_STA_0;
			}
		    break;
		default:
		    break;
	}

}



#endif